/**
 * 药品库
 */
Lib = cc.Sprite.extend({
	libArr: [],
	openFlag:false,
	doing:false,
	rm : 5,
	ctor:function(p){
		this._super("#lib_back.png");
		p.addChild(this,30);
		this.setTag(TAG_LIB);
		this.init();
	},
	init:function(){
		this.libArr = [];
		this.openFlag = false;
		this.setPosition(gg.width + this.width * 0.5 + 9,550+30);

		var beaker = new LibButton(this, 10, TAG_LIB_BEAKER,"#beaker/beaker.png", this.callback);
		beaker.setPosition(30, this.height * 0.5);
		
		var baohe = new LibButton(this, 10, TAG_LIB_BAOHE,"#baohe.png", this.callback);
		baohe.right(beaker, this.rm);
		
		var catheter = new LibButton(this, 10, TAG_LIB_CATHETER,"#catheter.png", this.callback);
		catheter.right(baohe, this.rm);
				
		var nh3 = new LibButton(this, 10, TAG_LIB_NH3,"#nh3.png", this.callback);
		nh3.right(catheter, this.rm);
		
		var co2 = new LibButton(this, 10, TAG_LIB_CO2,"#co2.png", this.callback);
		co2.right(nh3, this.rm);
				
		var rod = new LibButton(this, 10, TAG_LIB_ROD,"#rod.png", this.callback);
		rod.right(co2, this.rm);

		var nacl = new LibButton(this, 10, TAG_LIB_NACL,"#NaCl.png", this.callback);
		nacl.right(rod, this.rm);
		
		var filter = new LibButton(this, 10, TAG_LIB_FILTER,"#filter_paper1.png", this.callback);
		filter.right(nacl, this.rm);
	},
	moveLib:function(tag,width){
		width = 77;
		var begin = false;
		for(var i in this.libArr){
			var libTag = this.libArr[i];
			if(tag == libTag){
				begin = true;
			}
			if(begin){
				var lib = this.getChildByTag(libTag);
				if(lib != null){
					lib.runAction(cc.moveBy(0.5,cc.p(-width, 0)));
				}
			}
		}
	},
	callback:function(p){
		var pos = this.getPosition(); 
		var action = gg.flow.flow.action;
		switch(p.getTag()){
		case TAG_LIB_BAOHE:
			ll.run.loadBaohe(pos);
			break;
		case TAG_LIB_BEAKER:
			ll.run.loadBeaker(pos);
			break;
		case TAG_LIB_CATHETER:
			ll.run.loadCatheter(pos);
			break;
		case TAG_LIB_ROD:
			ll.run.loadRod(pos);
			break;
		case TAG_LIB_NH3:
			ll.run.loadGas(pos);
			break;
		case TAG_LIB_CO2:
			ll.run.loadGas(pos);
			break;
		case TAG_LIB_NACL:
			ll.run.loadNacl(pos);
			break;			
		case TAG_LIB_FILTER:
			ll.run.loadFilter(pos);
			break;
			default:
				break;
		}
		if(action == ACTION_NONE){
			this.moveLib(p.getTag(), p.width * p.getScale());
			p.removeFromParent(true);
		}
	},
	isOpen:function(){
		return this.openFlag; 
	},
	open:function(){
		if(this.openFlag || this.doing){
			return;
		}
		this.doing = true;
		var move = cc.moveBy(0.4, cc.p(-this.width,0));
		var func = cc.callFunc(function(){
			this.openFlag = true;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			 if(tag instanceof Array){
				 if(TAG_LIB_MIN < tag[1]){
					 // 显示箭头
					 gg.flow.location();
				 }
			 }
		}, this);
		var seq = cc.sequence(move,func);
		this.runAction(seq);
	},
	close:function(){
		if(!this.openFlag || this.doing){
			return;
		}
		this.doing = true;
		var move = cc.moveBy(0.4, cc.p(this.width,0));
		var func = cc.callFunc(function(){
			this.openFlag = false;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			if(tag instanceof Array){
				if(TAG_LIB_MIN < tag[1]){
//					// 隐藏箭头
//					ll.tip.arr.out();
					//箭头指向按钮
					ll.tip.arr.setPosition(gg.width-45,455);
				}
			}
		}, this);
		var seq = cc.sequence(move,func);
		this.runAction(seq);
	}
});
TAG_LIB_MIN = 30000;
////
TAG_LIB_BAOHE = 40000;
TAG_LIB_BEAKER=40001;
TAG_LIB_CATHETER=40002;
TAG_LIB_ROD=40003;
TAG_LIB_CO2=40004;
TAG_LIB_NH3=40005;
TAG_LIB_NACL=40006;
TAG_LIB_FILTER=40007;
libRelArr = [
             {tag:TAG_LIB_MIN, name:""},
             {tag:TAG_LIB_BAOHE, name:"饱和食盐水"},
             {tag:TAG_LIB_BEAKER, name:"烧杯"},
             {tag:TAG_LIB_CATHETER, name:"导管"},
             {tag:TAG_LIB_CO2, name:"二氧化碳"},
             {tag:TAG_LIB_NH3, name:"氨气"},
             {tag:TAG_LIB_ROD, name:"玻璃棒"},
             {tag:TAG_LIB_NACL, name:"精盐"}	,
             {tag:TAG_LIB_FILTER, name:"滤纸"}		];